package panel.recommendPathList
{
	import assists.Lang;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class SysPathList extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var dataList:Array;
		private var boxList:Array;
		
		private var startPoint:Point;
		
		private var _onShowDetail:Function;
		
		public function SysPathList(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;	
			
			dataList = [];
			boxList = [];
			
			addEvent();
		}
		
		
		private function addEvent():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		
		private function removeEvent():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		
		protected function upHandler(e:MouseEvent):void
		{
			var loc:Point = new Point(e.stageX,e.stageY);
			localToGlobal(loc);
			
			if(e.target.parent is SysPathBox && checkDistance(loc,startPoint)){
				
				e.stopImmediatePropagation();
				if(_onShowDetail is Function){
					_onShowDetail(e.target.parent.info);
				}
			}		
			startPoint = null;
		}
		
		
		private function checkDistance(p1:Point,p2:Point):Boolean
		{
			if(p2 != null){
				var dis:Number = 0;
				dis = Math.sqrt(Math.abs(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2)));
				var bool:Boolean;
				
				if(dis > (_stage.stageHeight * .01)){
					return false;
				}
				return true;
			}
			return false;
		}
		
		protected function downHandler(e:MouseEvent):void
		{
			e.stopImmediatePropagation();
			
			if(e.target.parent is SysPathBox){
				if(startPoint == null){
					startPoint = new Point(e.stageX,e.stageY);
				}else{
					startPoint.x = e.stageX;
					startPoint.y = e.stageY;
				}
				localToGlobal(startPoint);
			}
		}
		
		public function changeData(list:Array):void
		{
			dataList = list;
			
			addBox();
		}
		
		private function addBox():void
		{
			var pathTitleBox:SysPathBox;
			for(var i:int = 0;i < dataList.length;i++){
				pathTitleBox = new SysPathBox(_stage,_loader,_lang,_uri);
				pathTitleBox.changeData(dataList[i]);
				
				this.addChild(pathTitleBox);
				pathTitleBox.x = 0;
				pathTitleBox.y = pathTitleBox.height * i;
				
				boxList.push(pathTitleBox);
			}
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				for(var i:int = 0;i < boxList.length;i++){
					boxList[i].clear();
					this.removeChild(boxList[i]);
				}
				
				removeEvent();
			}
		}
		
		public function set showDetail(func:Function):void
		{
			_onShowDetail = func;
		}
	}
}